#!/bin/bash

# This script stats the dclog. The input is dclog, and output is the url status of all domians!

#0. clean the log, delete the log contains parenthesis

#sed '/(.*)/d' dclog.log > temp

#1. get all the url status

awk '{print $7$8}' dclog.log | sort | uniq | tr '\n' ' ' > status 

#2. stat the domain status
awk ' #
BEGIN {
    #get all url status
    getline status < "status";    
    sLen = split(status, statusArray);
    #get the status key array
    for (i = 1; i  <= sLen; ++i) {
        statusKeys[statusArray[i]] = i;
    }

} #BEGIN END

#main process, stat the url status of all domains!

{
    #get domain
    split($9, urlArray, "/");
    statusTemp = $7$8;
    if(!(urlArray[1] in ret)) {
        for (i = 1; i <= sLen; ++i) {
            if (1 == i)retTemp = 0;
            else retTemp = retTemp"\t"0;
        }
        ret[urlArray[1]] = retTemp;
    }

    split(ret[urlArray[1]], urlStatTemp, "\t");
    statusKey = statusKeys[statusTemp];
    ++urlStatTemp[statusKey];
    for (i = 1; i <= sLen; ++i) {
        if (1 == i)retTemp = urlStatTemp[i];
        else retTemp = retTemp"\t"urlStatTemp[i];
    }
    ret[urlArray[1]] = retTemp;
}

#PRINT THE RESULT
END {
    print "The stat result:";

    printf("\t\t");
    for (i=1; i <= sLen; ++i) {
        printf("%s\t",statusArray[I]);
    }
    printf("\n");
    for (domain in ret) {
        printf("%s\t\t%s\N", domain, ret[domain]);
    }
} 
' dclog.log
